package com.hello.sky.controller.user;

import com.hello.sky.constant.StatusConstant;
import com.hello.sky.model.entity.Category;
import com.hello.sky.result.PageResult;
import com.hello.sky.result.Result;
import com.hello.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController("userCategoryController")
@RequestMapping("/user/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public Result getCategoryByType(@RequestParam(value = "type", required = false) Integer type){
        List<Category> listByType = categoryService.getListByType(type);
        //由于和admin共用一个service，所以这里要过滤掉未启用的
        List<Category> collect = listByType.stream().filter(category -> category.getStatus() == StatusConstant.ENABLE).collect(Collectors.toList());
        return Result.success(collect);
    }
}
